package com.scs.application.modules.upms.wxmp.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.modules.upms.entity.WxmpMember;
import com.scs.application.modules.upms.service.WxmpMemberService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 */
@AllArgsConstructor
@Component
public class UnsubscribeHandler extends AbstractHandler {

    private final WxmpMemberService wxmpMemberService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        String openId = wxMessage.getFromUser();
        this.logger.info("取消关注用户 OPENID: " + openId);

        wxmpMemberService.remove(Wrappers.<WxmpMember>query().eq("open_id", openId));
        return null;
    }

}
